<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/9/11
 * Time: 18:00
 */

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';
include $RootDir.'/utils/CommonConstant.php';
include $RootDir.'/utils/FileUtils.php';

$user_id=$_POST['user_id']; //手机号码
$imageSize=$_POST['imageSize'];
$head_local_path=$_POST['headlocalpath'];

$conn = conn_mysql();

$fileUtils = new FileUtils();
$segment = floor($user_id / 1000);
$prePath = IMAGE_ROOT_PATH . $segment . '/';
$headFileName = $prePath . $user_id . '.jpg';
$headTmpFileName = $prePath . $user_id . '_tmp.jpg';
$netHeadFileName = "/images/" . $segment . '/' . $user_id . '.jpg';    //保存的是相对地址，前缀是网络地址，非物理地址
$saveHead = False;
if ($imageSize == 1) {
    if ($fileUtils->createDir($prePath)) {
        if (file_exists($headFileName)) $fileUtils->moveFile($headFileName, $headTmpFileName,True);
        if (move_uploaded_file($_FILES[HTTPS_IMAGE_NAME_PRE]['tmp_name'], $headFileName)) {
            $query = "update user_info set head_path = '$netHeadFileName' where user_id = '$user_id'";
            if (!mysql_query($query,$conn)) {
                die('err: ' . mysql_error());
            }
            $saveHead = True;
            if (file_exists($headTmpFileName)) unlink($headTmpFileName);
        }else die('err: 保存图片失败');
    }else die('err: 创建保存图片路径失败');
}

if($saveHead)echo 'OK';

mysql_close($conn);